package com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.organization;

import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.SalesPlanStatAtomAbstract;
import com.alibaba.citrus.cr.sales.plan.facade.service.statistics.stat.model.SalesPlanStatRequest;
import com.alibaba.cz.base.tool.log.Log;


import java.util.concurrent.CountDownLatch;

/**
 * @author luyi
 * @date 2021/12/2 11:23 下午
 */
public class OrgStatService extends SalesPlanStatAtomAbstract {

    private static final Log log = Log.getLogger(OrgStatService.class);

    public OrgStatService(SalesPlanStatRequest statRequest, CountDownLatch latch){
        super.statRequest = statRequest;
        super.latch = latch;
    }

    @Override
    protected void handle(SalesPlanStatRequest statRequest) {
        try {
            orgCollect(statRequest);
        } catch (InterruptedException e) {
            log.error("OrgStatService.InterruptedException:",e);
        }
    }

    @Override
    protected String taskName() {
        return "orgStatService.gather";
    }

    /**
     * 组织维度汇总
     */
    public void orgCollect(SalesPlanStatRequest statRequest) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        salesPlanTaskExecutor.execute(new OrganizationEmployeeStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationDepartmentStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationChannelStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new OrganizationBusinessUnitStatService(statRequest,countDownLatch));
        countDownLatch.await();
    }
}
